public class Test {
    private StringBuilder stringbuilder = new StringBuilder();
    public void treestrHelper(TreeNode root) {
        if (root == null) {
            return;
        }
        stringbuilder.append("(");
        stringbuilder.append(root.val);
        treestrHelper(root.left);
        if (root.left == null &&root.right != null) {
            stringbuilder.append("()");
        }
        treestrHelper(root.right);
        stringbuilder.append(")");
    }

    public String tree2str(TreeNode root) {
        stringbuilder = new StringBuilder();
        if (root == null) {
            return "";
        }
        tree2str(root);
        stringbuilder.deleteCharAt(0);
        stringbuilder.deleteCharAt(stringbuilder.length() - 1);
        return stringbuilder.toString();

    }

    public static void main(String[] args) {
        Test t = new Test();

    }
}

class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
}
